package com.goldsprite.microhorigame.ecs;

import com.goldsprite.gameframeworks.ecs.component.*;
import com.badlogic.gdx.math.*;
import com.goldsprite.gameframeworks.ecs.entity.GObject;

public class EntityPropsComponent extends Component {
	private GObject nearCampfire;
	public final float campFireHealingAmount = 2f;
	private float oriSpeed = 1.8f * 60f *2f, speed = oriSpeed;
	private float boostSpeedMultiplier = 3f;
	private float jumpForce = 12f * 60f;
	private float maxHealth = 100;
	private float health = maxHealth;
	private float damage = 20;

	private boolean isSpeedBoost;

	public void move(float moveDir) {
		move(moveDir, isSpeedBoost ? boostSpeedMultiplier : 1);
	}
	public void move(float moveDir, float boost) {
		//转向
		turn(moveDir);
		RigidbodyComponent rigi = getComponent(RigidbodyComponent.class);
		//移动
		if(rigi != null){
			rigi.setVelocity(moveDir * speed * boost, rigi.getVelocity().y);
		}
	}
	public void turn(float dirX) {
		int norMoveDirX = (int)Math.signum(dirX);
		if(norMoveDirX != 0) setFacing(norMoveDirX);
	}

	public void hurt(float damage) {
		health = MathUtils.clamp(health - damage, 0, maxHealth);
	}

	public void jump() {
		RigidbodyComponent rigi = getComponent(RigidbodyComponent.class);
		if(rigi != null)
			rigi.setVelocity(rigi.getVelocity().x, jumpForce);
	}

	public int getFacing() {
		return fields.transform.getFace().intX;
	}
	public void setFacing(int facing) {
		getTransform().getFace().setX(facing);
	}


	public float getMaxHealth() {
		return maxHealth;
	}

	public void setMaxHealth(float maxHealth, boolean isFull) {
		this.maxHealth = maxHealth;
		if (isFull) this.health = maxHealth;
	}
	public void setMaxHealth(float maxHealth) {
		setMaxHealth(maxHealth, false);
	}

	public float getHealth() {
		return health;
	}

	public void setHealth(float nowHealth) {
		health = MathUtils.clamp(nowHealth, 0, maxHealth);
	}

	public void setDamage(float damage) {
		this.damage = damage;
	}
	public float getDamage() {
		return damage;
	}

	public boolean isDead() {
		return health <= 0;
	}


	public float getOriSpeed(){
		return oriSpeed;
	}
	public void setOriSpeed(float speed) {
		this.oriSpeed = this.speed = speed;
	}
	public float getSpeed() {
		//返回正常或疾跑速度
		return  speed * (!isSpeedBoost ? 1 : boostSpeedMultiplier);
	}
	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public void setSpeedBoost(boolean boostSpeedMultiplier) {
		isSpeedBoost = boostSpeedMultiplier;
	}
	public boolean isSpeedBoost() {
		return isSpeedBoost;
	}

	public void setNearCampfire(GObject nearCampfire) {
		this.nearCampfire = nearCampfire;
	}
	public GObject getNearCampfire() {
		return nearCampfire;
	}
}
